package com.jarvan.controller;

import com.jarvan.dto.TicketVo;
import com.jarvan.enums.ResultEnum;
import com.jarvan.exception.TrainException;
import com.jarvan.model.Ticket;
import com.jarvan.service.impl.TicketServiceImpl;
import com.jarvan.utils.ResultVoUtil;
import com.jarvan.vo.ResultVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(value = "后台车票增删改查", tags = {"AdminTicket"})
@Slf4j
@RestController
@RequestMapping("/admin/ticket")
public class AdminTicketController {

    @Autowired
    private TicketServiceImpl ticketService;

    @GetMapping("/findByBuyerId")
    public ResultVo<TicketVo> findByBuyerId(@RequestParam("buyerId") Integer buyerId,
                                            @RequestParam(name = "page", defaultValue = "1") Integer page,
                                            @RequestParam(name = "size", defaultValue = "10") Integer size){
        if (buyerId == null){
            log.error("[订单查询]  通过用户ID查询订单  未传用户id");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Page<Ticket> ticketPage = ticketService.findAllByBuyerId(buyerId,PageRequest.of(page - 1, size));
        if (ticketPage.isEmpty()){
            log.warn("[订单查询]  该用户订单列表为空");
            return ResultVoUtil.error(ResultEnum.TICKET_LIST_ISEMPTY.getCode(), ResultEnum.TICKET_LIST_ISEMPTY.getMessage());
        }
        TicketVo ticketVo = new TicketVo();
        ticketVo.setTicketList(ticketPage.getContent());
        ticketVo.setTotal(ticketPage.getTotalElements());
        return ResultVoUtil.success(ticketVo);
    }

    @GetMapping("/findAll")
    public ResultVo<TicketVo> findAllTicket(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                          @RequestParam(name = "size", defaultValue = "10") Integer size){
        Page<Ticket> ticketPage = ticketService.findAll(PageRequest.of(page - 1, size));
        if (ticketPage.isEmpty()){
            log.warn("[订单查询]  订单列表为空");
            return ResultVoUtil.error(ResultEnum.TICKET_LIST_ISEMPTY.getCode(), ResultEnum.TICKET_LIST_ISEMPTY.getMessage());
        }
        TicketVo ticketVo = new TicketVo();
        ticketVo.setTicketList(ticketPage.getContent());
        ticketVo.setTotal(ticketPage.getTotalElements());
        return ResultVoUtil.success(ticketVo);
    }

    @GetMapping("/findByStatus")
    public ResultVo<TicketVo> findAllByStatus(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                           @RequestParam(name = "size", defaultValue = "10") Integer size,
                                           @RequestParam("status") Integer status){
        if (status == null){
            log.error("[车票查询]  根据车票状态查询  status未传");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Page<Ticket> ticketPage = ticketService.findAllByTicketStatus(PageRequest.of(page - 1, size), status);
        if (ticketPage.isEmpty()){
            log.warn("[订单查询]  状态所属订单为空");
            return ResultVoUtil.error(ResultEnum.TICKET_IS_EMPTY.getCode(), ResultEnum.TICKET_IS_EMPTY.getMessage());
        }
        TicketVo ticketVo = new TicketVo();
        ticketVo.setTicketList(ticketPage.getContent());
        ticketVo.setTotal(ticketPage.getTotalElements());
        return ResultVoUtil.success(ticketVo);
    }

    @GetMapping("/findByTicketId")
    public ResultVo<TicketVo> findByTicketId(@RequestParam("ticketId") String ticketId){
        if (ticketId.isEmpty()){
            log.error("[车票查询]  根据车票ID查询  ticketId未传");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Ticket ticket = ticketService.findById(ticketId);
        if (ticket == null){
            log.warn("[订单查询]  订单不存在");
            return ResultVoUtil.error(ResultEnum.TICKET_NOT_EXIST.getCode(), ResultEnum.TICKET_NOT_EXIST.getMessage());
        }
        List<Ticket> tickets = new ArrayList<>();
        tickets.add(ticket);
        TicketVo ticketVo = new TicketVo();
        ticketVo.setTicketList(tickets);
        ticketVo.setTotal((long)1);
        return ResultVoUtil.success(ticketVo);
    }

    @GetMapping("/findByPayStatus")
    public ResultVo<TicketVo> findByPayStatus(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                              @RequestParam(name = "size", defaultValue = "10") Integer size,
                                              @RequestParam(name = "payStatus") Integer payStatus){
        if (payStatus == null){
            log.error("[车票查询]  根据车票支付状态查询  payStatus未传");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Page<Ticket> ticketPage = ticketService.findAllByPayStatus(payStatus, PageRequest.of(page - 1, size));
        if (ticketPage.isEmpty()){
            log.error("[订单查询] 支付状态查询 未查询到数据");
            return ResultVoUtil.error(ResultEnum.TICKET_IS_EMPTY.getCode(), ResultEnum.TICKET_IS_EMPTY.getMessage());
        }
        TicketVo ticketVo = new TicketVo();
        ticketVo.setTicketList(ticketPage.getContent());
        ticketVo.setTotal(ticketPage.getTotalElements());
        return ResultVoUtil.success(ticketVo);
    }
}
